package com.guiwu.gteway.industryapp.util;

import android.content.Context;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.model.LatLng;
import com.guiwu.gteway.industryapp.R;

/**
 * Created by 乔冠 on 2017/12/21. 百度定位工具类
 */

public class BaiduLocation {
    private Context context;
    private LocationClient mLocationClient;
    private boolean isFirstLoc;
    private MapView mapView;
    private BaiduMap mBaiduMap;

    public BaiduLocation(Context context,BaiduMap baiduMap,MapView mapView,boolean isFirstLoc){
        this.context =context;
        this.isFirstLoc = isFirstLoc;
        this.mapView = mapView;
        this.mBaiduMap = baiduMap;

        initMapLocation();
    }

    /**
     * 设置map定位
     */
    private void initMapLocation(){
        mLocationClient = new LocationClient(context);
        setLocationOption();
        mLocationClient.registerLocationListener(new MylocationListener());
        requestLocation();
    }

    /**
     * 设置定位参数
     */
    private void setLocationOption(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setScanSpan(3000);
        option.setCoorType("bd09ll");
        option.setOpenGps(true);
        option.setIgnoreKillProcess(true);
        mLocationClient.setLocOption(option);
    }

    /**
     * 开始定位
     */
    private void requestLocation(){
        mLocationClient.start();
    }

    /**
     * 停止定位
     */
    public void stopLocation(){
        mLocationClient.stop();
    }


    /**
     * 定义定位回调类
     */
    private class MylocationListener extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {

            // map view 销毁后不在处理新接收的位置
            if (location == null || mapView == null) {
                return;
            }

            StringBuilder currentPostion = new StringBuilder();
            currentPostion.append("纬度：").append(location.getLatitude()).append("\n");
            currentPostion.append("经线：").append(location.getLongitude()).append("\n");
            currentPostion.append("定位方式");
            if (location.getLocType() == BDLocation.TypeGpsLocation){
                currentPostion.append("GPS");
            }else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
                currentPostion.append("网络");
            }

            MyLog.i("location",currentPostion.toString());

//               mCurrentLat = location.getLatitude();
//                    mCurrentLon = location.getLongitude();
//                    mCurrentAccracy = location.getRadius();
//                    locData = new MyLocationData.Builder()
//                            .accuracy(location.getRadius())
//                            // 此处设置开发者获取到的方向信息，顺时针0-360
//                            .direction(mCurrentDirection).latitude(location.getLatitude())
//                            .longitude(location.getLongitude()).build();
//                    mBaiduMap.setMyLocationData(locData);
                    if (isFirstLoc) {
                        isFirstLoc = false;
                        LatLng ll = new LatLng(location.getLatitude(),
                                location.getLongitude());
                        MapStatus.Builder builder = new MapStatus.Builder();
                        builder.target(ll).zoom(18.0f);
                        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
                    }
                }

        }


}
